package com.xncoding.pos.controller;

import cn.hutool.json.JSONUtil;
import com.xncoding.pos.common.model.ResultGenerator;
import com.xncoding.pos.common.model.ResultModel;
import com.xncoding.pos.model.TbUser;
import com.xncoding.pos.service.thread.AsyncTaskService;
import com.xncoding.pos.service.thread.CompletableService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@Slf4j
public class CompletableFutureController {

    @Resource
    AsyncTaskService asyncTaskService;

    @Resource
    CompletableService completableService;


    @GetMapping("/completableFutureTest_any")
    @ApiOperation("等待所其中任意一个任务执行完")
    public ResultModel completableFutureTest() {
        this.completableService.completableFutureTest_anyDone();
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/completableFutureTest_all")
    @ApiOperation("等待所有任务完成_等待所有任务完成")
    public ResultModel completableFutureTest_all() {
        this.completableService.completableFutureTest_allDone();
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/completableFutureTestAllDone")
    @ApiOperation("等待所有任务完成")
    public ResultModel completableFutureTestAllDone() {
        this.completableService.ThreadAllDown();
        return ResultGenerator.genSuccessResult();
    }


    @GetMapping("/completableFutureTest1")
    @ApiOperation("串行执行1")
    public ResultModel completableFutureTest1() {

        this.completableService.completableFutureTest1();
        return ResultGenerator.genSuccessResult();
    }


    @GetMapping("/completableFutureTest2")
    @ApiOperation("串行执行2")
    public ResultModel completableFutureTest2() {

        TbUser tbUser = this.completableService.completableFutureTest2();
        log.info("completableFutureTest2-----打印日志------{}", JSONUtil.toJsonStr(tbUser));
        return ResultGenerator.genSuccessResult();
    }


    @GetMapping("/asyncProcessWithError")
    @ApiOperation("CompletableFuture执行报错的场景")
    public ResultModel asyncProcessWithHandling() {
        CompletableFuture<String> stringCompletableFuture = completableService.asyncProcessWithHandling();
        try {
            log.info("asyncProcessWithError-----打印日志------{}", stringCompletableFuture.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/completableFutureTest5")
    @ApiOperation("串行执行5")
    public ResultModel completableFutureTest5() {

        this.completableService.completableFutureTest5();
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/completableFutureTest6")
    @ApiOperation("串行执行6")
    public ResultModel completableFutureTest6() {

        this.completableService.completableFutureTest6();
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/completableFutureTest7")
    @ApiOperation("串行执行7")
    public ResultModel completableFutureTest7() {

        this.completableService.completableFutureTest7();
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/completableFutureTest8")
    @ApiOperation("串行执行8")
    public ResultModel completableFutureTest8() {
        this.completableService.completableFutureTest8();
        return ResultGenerator.genSuccessResult();
    }


    @GetMapping("/completableFutureTest9")
    @ApiOperation("串行执行9")
    public ResultModel completableFutureTest9() {
        this.completableService.completableFutureTest9();
        return ResultGenerator.genSuccessResult();
    }


    @GetMapping("/completionServiceTest1")
    @ApiOperation("多任务执行，获取所有返回值")
    public ResultModel completionServiceTest1() {
        asyncTaskService.completionServiceTest1();
        return ResultGenerator.genSuccessResult();
    }


    @GetMapping("/completionForPollTest")
    @ApiOperation("多任务执行，获取返回值（超时机制）")
    public ResultModel completionForPollTest() {
        asyncTaskService.completionForPollTest();
        return ResultGenerator.genSuccessResult();
    }



}
